//
//  XWNetworkConfig.swift
//  iOS-XiaWan
//
//  Created by Miles on 2023/10/10.
//  Copyright © 2023 turningsyn. All rights reserved.
//

import Foundation



//MARK: - 当前项目环境调整位置
public var kEnvironmentType: EnvironmentType {
    let typeValue = UserDefaults.standard.string(forKey: kXWEnvironmentTypeCacheKey)
    if let typeV = typeValue, let type = EnvironmentType(rawValue: typeV) {
        return type
    }
#if DEBUG
    return .DEVELOP
#else
    return .RELEASE
#endif
}


public let kGroupUserDefaultsUidKey = "xw_uid"
public let kGroupUserDefaultsAccessTokenKey = "xw_accessToken"

public var BaseURL_DEVELOP : String  = ""
public var BaseURL_RELEASE : String  = ""
private var CACHE_USER_ID : String?
private var ACCESS_TOKEN : String?
private var SUITE_NAME : String?



//MARK: - 基础URL
var BaseURL: String {
    
    // 根据环境不同，作不同的判断
    switch kEnvironmentType {
        
    case .DEVELOP:
        return BaseURL_DEVELOP
    
    case .RELEASE:
        return BaseURL_RELEASE
    }
}


//MARK: - SocketURL
public var SocketURL: String {
    let userId = getLoginUserId()
    let platform = getPlatform()
    let host0 = BaseURL.replacingOccurrences(of: "http://", with: "")
    let host1 = host0.replacingOccurrences(of: "https://", with: "")
    let host2 = host1.replacingOccurrences(of: "/rework-api", with: "")
    let sUrl = "wss://" + host2 + "/ws?clientId=" + userId + "&" + "platform=" + platform
    return sUrl
}

public func getGroupSuiteName () -> String {
    if let suiteName = SUITE_NAME {
        return suiteName
    }else {
#if os(iOS)
        return "group.com.turingSyn.iOS-XiaWan"
#elseif os(macOS)
        return "group.com.turingSyn.Mac-XiaWan"
#endif
    }
}

public func setGroupSuiteName(suiteName: String) {
    SUITE_NAME = suiteName
}

public func getLoginUserId () -> String {
    if let userID = CACHE_USER_ID {
        return userID
    }else {
        let suiteName = getGroupSuiteName()
        if let userDefaults = UserDefaults(suiteName: suiteName) {
            if let xwUid = userDefaults.object(forKey: kGroupUserDefaultsUidKey) as? String {
                CACHE_USER_ID = xwUid
            }
        }
        return CACHE_USER_ID ?? "";
    }
}

public func getAccessToken () -> String {
    if let accessToken = ACCESS_TOKEN {
        return accessToken
    }else {
        let suiteName = getGroupSuiteName()
        if let userDefaults = UserDefaults(suiteName: suiteName) {
            if let accessToken = userDefaults.object(forKey: kGroupUserDefaultsAccessTokenKey) as? String {
                ACCESS_TOKEN = accessToken
            }
        }
        return ACCESS_TOKEN ?? "";
    }
}

// 框架外部自定设置user ID
public func setLoginUserId (userID: String) {
    CACHE_USER_ID = userID
    let suiteName = getGroupSuiteName()
    if let userDefaults = UserDefaults(suiteName: suiteName) {
        if let _ = CACHE_USER_ID {
            userDefaults.setValue(CACHE_USER_ID, forKey: kGroupUserDefaultsUidKey)
            userDefaults.synchronize()
        }else {
            userDefaults.removeObject(forKey: kGroupUserDefaultsUidKey)
            userDefaults.synchronize()
        }
    }
}

public func setAccessToken (accessToken: String) {
    ACCESS_TOKEN = accessToken
    let suiteName = getGroupSuiteName()
    if let userDefaults = UserDefaults(suiteName: suiteName) {
        if let _ = ACCESS_TOKEN {
            userDefaults.setValue(ACCESS_TOKEN, forKey: kGroupUserDefaultsAccessTokenKey)
            userDefaults.synchronize()
        }else {
            userDefaults.removeObject(forKey: kGroupUserDefaultsAccessTokenKey)
            userDefaults.synchronize()
        }
    }
}

//MARK: - 运行环境
public enum EnvironmentType: String, CaseIterable {
    case DEVELOP = "开发环境"
    case RELEASE = "测试环境"
}
